package com.kolon.business.mAccessMan.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mAccessMan.controller.MAccessManApplySearchPopController")
public class MAccessManApplySearchPopController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mAccessMan/mAccessManApplySearchPop.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String companyCD = (String)userMap.get("CompanyCD");
		String deptCode = (String)userMap.get("DeptCode");	
		String keyword = ComUtil.nullChk(request.getParameter("txt_visitor_name"));
		Map model = new HashMap();
		StringBuffer buffer1 = new StringBuffer();
		buffer1.append(url);
		buffer1.append("/access_man.aspx?command=searchhuman");
		buffer1.append("&keyword=").append(keyword);
		buffer1.append("&companyCD=").append(companyCD);
		buffer1.append("&deptCD=").append(deptCode);
		// XML DOCUMENT 및 인증 처리
		Document doc1 = ComUtil.getDocument(buffer1.toString(), id, pw);
		Element element1 = doc1.getRootElement();
		List xmlList1 = element1.getChildren("dept");
		Iterator itXml1 = xmlList1.iterator();
		List mapList1 = new ArrayList();
		Map map1 = new HashMap();
		// XML 데이터 
		while (itXml1.hasNext()) {
			Element el = (Element)itXml1.next();
			map1 = new HashMap();
			map1.put("name", el.getChild("name").getText());
			map1.put("dept", el.getChild("dept").getText());
			if (el.getChild("title") != null) map1.put("title", el.getChild("title").getText());
			else map1.put("title","");
			if (el.getChild("phone") != null) map1.put("phone", el.getChild("phone").getText());
			else map1.put("phone","");
			map1.put("gubun", el.getChild("gubun").getText());
			mapList1.add(map1);
		}		
		model.put("list1",mapList1);
		List xmlList2 = element1.getChildren("visitor");
		Iterator itXml2 = xmlList2.iterator();
		List mapList2 = new ArrayList();
		Map map2 = new HashMap();
		// XML 데이터 
		while (itXml2.hasNext()) {
			Element el = (Element)itXml2.next();
			map2 = new HashMap();
			map2.put("name", el.getChild("name").getText());
			map2.put("dept", el.getChild("dept").getText());
			if (el.getChild("title") != null) map2.put("title", el.getChild("title").getText());
			else map2.put("title","");
			if (el.getChild("phone") != null) map2.put("phone", el.getChild("phone").getText());
			else map2.put("phone","");
			map2.put("gubun", el.getChild("gubun").getText());
			mapList2.add(map2);
		}
		model.put("list2",mapList2);
		// JSON 리턴
		return new ModelAndView("jsonView", model);
	}
}